﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrexSpace.Business
{
    public class Repository : IRepository
    {
        public IQueryable<Post> AllPosts()
        {
            using (sysrwEntities oen = new sysrwEntities())
            {
                var posts = oen.Posts.Select(x => x);
                return posts;
            }
        }

        public IQueryable<Post> FindPostsByUser(string accountId)
        {
            using (sysrwEntities oen = new sysrwEntities())
            {
             var posts =   oen.Posts.Where(x => x.User_Id  == accountId);
             return posts;
            }
        }

        public Post GetPost(string id)
        {
               using (sysrwEntities oen = new sysrwEntities())
               {
                   var post = oen.Posts.Where(x => x.Id == id).First();
                   return post;
               }
        }

        public void AddPost(Post post)
        {
               using (sysrwEntities oen = new sysrwEntities())
               {
                   oen.AddToPosts(post);
               }
        }

        public void DeletePost(Post post)
        {
               using (sysrwEntities oen = new sysrwEntities())
               {
                   oen.DeleteObject(post);
               }
        }

      

        public IQueryable<Category> AllCategories()
        {
            
            using (sysrwEntities oen = new sysrwEntities())
            {
                var rtnQuery = oen.Categories.Select(x => x);
                return rtnQuery;
            }
            
        }

        public void AddCategory(string ParentCategoryID , string newCategoryName )
        {
            using (sysrwEntities oen = new sysrwEntities())
            {   
                Category newCat = new Category();
                newCat.Parent = ParentCategoryID ;
                newCat.Name = newCategoryName;
                oen.Categories.AddObject(newCat);
            }
        }

        public void DeleteCategory(Category category)
        {
            using (sysrwEntities oen = new sysrwEntities())
            {
                oen.DeleteObject(category);
            }
        }

        public void Save()
        {
            using (sysrwEntities oen = new sysrwEntities())
            {
                oen.SaveChanges();
            }
        }
    }
}
